<?php

class Blog_ArticulosController extends Zend_Controller_Action
{

    protected $_articulos = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->cliente = Zend_Auth::getInstance()->getIdentity();
        $this->_articulos = new Blog_Model_DbTable_Articulos();
        $this->view->titulo = "Blog";
    }

    public function indexAction()
    {
        // action body
        $this->_redirect('/blog');
    }

    public function huerfanosAction()
    {
        // action body
        $this->view->articulos = $this->_articulos->obtenerHuerfanos();
    }

    public function crearAction()
    {
        $form = new Blog_Form_Articulo();
                
        if( $this->getRequest()->isPost() ){
            
            $img_elmnt = $form->getElement('imagen');
            $art_id = $this->_articulos->obtenerSigId();
            $img_elmnt->setDestination($this->_articulos->crearCarpeta($art_id));

            if( $form->isValid( $this->_getAllParams() ) ) {

                if ($form->imagen->receive()){ 
                    
                    $nombre = $form->getValue('nombre');
                    $titulo = $form->getValue('titulo');
                    $cuerpo = $form->getValue('cuerpo');
                    $creador = Zend_Auth::getInstance()->getIdentity();
                    $id = $this->_articulos->crearArticulo($nombre,$titulo,$cuerpo,$creador);
                    $imagen = $form->getValue('imagen');
                    $this->_articulos->guardarImagen($id,$imagen);
                    $this->_redirect('/blog');
                }
            }      
            $this->view->form = $form;    
        }elseif( $this->getRequest()->isGet() ){
            $this->view->form = $form;
        }
        
    }

    public function editarAction()
    {
        //http://danielbonhaure/blog/articulos/editar/id/#
        $id = $this->_getParam('id');
        $articulo = $this->_articulos->obtenerArticulo($id);
        $this->view->articulo = $articulo;
        
        $form = new Blog_Form_Articulo();
        $form->setAction('/blog/articulos/editar/id/' . $id);
        $form->setMethod('post');
        $submit = $form->getElement('Crear');
        $submit->setLabel('Guardar');
        $form->inicializar($articulo);
        $imagen = $form->getElement('imagen');
        $upload = Zend_Registry::get('upload_paths');
        $imagen->setDestination($upload->paths->upload->images->blog->articulos . "/" . $articulo->id);
        
        if( $this->getRequest()->isPost() ){
          
            if ($form->isValid( $this->_getAllParams() )) {
                if ($form->imagen->receive()){ 
                    $data = array(
                        'nombre' => $form->getValue('nombre'),
                        'titulo' => $form->getValue('titulo'),
                        'imagen' => $form->getValue('imagen'),
                        'path' => $imagen->getDestination(),
                        'cuerpo' => $form->getValue('cuerpo'),   
                        'mod' => Zend_Auth::getInstance()->getIdentity()->usuario,
                    );
                    $this->_articulos->actualizarArticulo($id,$data);
                    $this->_redirect('/blog/articulos/ver/id/' . $id);  
                }
            }

            $this->view->form = $form;
            
        }elseif( $this->getRequest()->isGet() ){
            
            $this->view->form = $form;
        }    
        
    }

    public function borrarAction()
    {
        //http://danielbonhaure/blog/articulos/borrar/id/*
        $cliente = Zend_Auth::getInstance()->getIdentity()->usuario;
        $role = Zend_Auth::getInstance()->getIdentity()->role;
        
        if ($this->getRequest()->isPost()) {
            $id = $this->getRequest()->getPost('id');
            $borrar = $this->getRequest()->getPost('borrar');
            if ($borrar == 'Si') {
                $duenho = $this->_articulos->obtenerArticulo($id)->creador;
                if ($cliente == $duenho || $role == "admin" || $role == "adminCarreras") {
                    $this->_articulos->borrarArticulo($id);
                    $this->_redirect('/blog'); 
                } else {
                    $this->_redirect('/error/permiso');  
                }
            } elseif ($borrar == "No") {
                $this->_redirect('/blog/articulos/ver/id/'.$id); 
            }
        } else {
            $id = $this->_getParam('id');
            $duenho = $this->_articulos->obtenerArticulo($id)->creador;
            if ($cliente == $duenho || $role == "admin" || $role == "adminCarreras") {
                $this->view->articulo = $this->_articulos->obtenerArticulo($id);
            } else {
                $this->_redirect('/error/permiso');  
            }
        }   
    }

    public function verAction()
    {
        //http://danielbonhaure/blog/articulos/ver/id/#
        $id = $this->_getParam('id');
        $articulo = $this->_articulos->obtenerArticulo($id);
        $this->view->articulo = $articulo;
    }


}













